package com.example.ch07_01_01listviewdemo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private ListView listView1;
	private String[] data;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHandler();
	}

	private void initView() {
		// TODO Auto-generated method stub
		listView1 = (ListView) this.findViewById(R.id.listView1);
		data = getResources().getStringArray(R.array.Colors);
		ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
		
		TextView headerText = new TextView(this);
		headerText.setTextColor(Color.BLUE);
		headerText.setText("All Colors");
		listView1.addHeaderView(headerText);
		TextView footerText = new TextView(this);
		footerText.setTextColor(Color.RED);
		footerText.setText("We have " +data.length+" colors");
		listView1.addFooterView(footerText);
		listView1.setAdapter(adapter);
	}

	private void initHandler() {
		// TODO Auto-generated method stub
		//listView1.setOnItemSelectedListener(this,
		/*
		listView1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String s = arg0.getItemAtPosition(arg2)+"";
				Toast.makeText(MainActivity.this, s,Toast.LENGTH_SHORT).show();
				return false;
			}
			
		});
		*/
		listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String s = arg0.getItemAtPosition(arg2)+"";
				Toast.makeText(MainActivity.this, s,Toast.LENGTH_SHORT).show();
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
